package panel.selfPathList
{
	import assists.HtmlText;
	import assists.Lang;
	import assists.TextFieldUtils;
	import assists.URI;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import struct.SelfPath;
	
	public class SelfPathListBox extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		public var info:SelfPath;
		
		private var titleText:TextField;
		private var timeText:TextField;
		
		private var backgroud:Bitmap;
		
		private var lockIcon:Sprite;
		private var rightIcon:Bitmap;
		private var deleteIcon:Bitmap;
		
		private var cover:Sprite;
		
		private var lock:Bitmap;
		private var lockOpen:Bitmap;
		
		private var onChangeThis:Function;
		
		public var rightSprite:ClickArea;
		public var deleteSprite:ClickArea;

		private var onShowDetail:Function;
		private var onDelteThis:Function;
		
		public function SelfPathListBox(stage:Stage, loader:ResourceLoader, lang:Lang, uri:URI)
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			initThis();
		}
		
		private function initThis():void
		{
			backgroud = new _loader.uiManager.componentBack as Bitmap;
			
			backgroud.width = _stage.stageWidth;
			backgroud.height = _stage.stageHeight * .15;
			this.addChild(backgroud);
			
			titleText = new TextField;
			titleText.mouseEnabled = true;
			titleText.selectable = false;
			titleText.wordWrap = false;
			titleText.multiline = false;
			this.addChild(titleText);
			titleText.x = 0;
			titleText.y = 20;
			
			timeText = new TextField;
			timeText.mouseEnabled = true;
			timeText.selectable = false;
			timeText.wordWrap = false;
			timeText.multiline = false;
			this.addChild(timeText);
			timeText.x = 0;
			timeText.y = backgroud.height - 20;
			
			lockIcon = new Sprite;
			lock = new _loader.uiManager.lock as Bitmap;
			lockOpen = new _loader.uiManager.lockOpen as Bitmap;
			
			lockIcon.addChild(lock);
			lockIcon.addChild(lockOpen);
			
			lockOpen.visible = false;
			
			lockIcon.buttonMode = true;
			lockIcon.addEventListener(MouseEvent.CLICK,openThisHandler);
			
			deleteIcon = new _loader.uiManager.deleteBtn as Bitmap;	
			deleteIcon.smoothing = true;
			deleteIcon.width = _stage.stageHeight * .06;
			deleteIcon.height = _stage.stageHeight * .06;
			
			deleteSprite = new ClickArea;
			deleteSprite.addChild(deleteIcon);
			
			deleteSprite.addEventListener(MouseEvent.CLICK,deleteHandler);
			
			rightIcon = new _loader.uiManager.arrowIcon as Bitmap;
			
			rightSprite = new ClickArea;
			rightSprite.addChild(rightIcon);
			repositionImg();
			
			rightSprite.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		
		protected function deleteHandler(event:MouseEvent):void
		{
			if(onDelteThis is Function){
				onDelteThis(this.info);
			}
		}
		
		protected function clickHandler(event:MouseEvent):void
		{
			if(onShowDetail is Function){
				onShowDetail(this.info);
			}
		}
		
		protected function openThisHandler(event:MouseEvent):void
		{
			event.stopImmediatePropagation();
			
			if(lock.visible == true){
				lock.visible = false;
				lockOpen.visible = true;
				
				if(onChangeThis is Function){
					onChangeThis(info,true);
				}
			}else{
				lock.visible = true;
				lockOpen.visible = false;
				
				if(onChangeThis is Function){
					onChangeThis(info,false);
				}
			}
		}
		
		private function repositionImg():void
		{
			if(_stage){
				
				this.addChild(rightSprite);
				this.addChild(lockIcon);
				this.addChild(deleteSprite);
				
				lockIcon.x = backgroud.height * .1;
				lockIcon.y = backgroud.height * .1;
				lockIcon.width = backgroud.height * .8;
				lockIcon.height = backgroud.height * .8;
				
				rightSprite.x = backgroud.width - rightSprite.width - backgroud.height * .1;
				rightSprite.y = (backgroud.height - rightSprite.height) / 2;
				
				deleteSprite.x = backgroud.width - deleteSprite.width - backgroud.height * .4 - rightSprite.width;
				deleteSprite .y = (backgroud.height - deleteSprite.height) / 2;
			}
		}
		
		public function changeData(obj:SelfPath):void
		{
			info = obj;
			
			TextFieldUtils.setHtmlText(titleText,"<b>" + HtmlText.formatSizeColor(obj.name,0x000000,20) + "</b>");
			titleText.width = titleText.textWidth + 5;
			titleText.height = titleText.textHeight + 5;
			
			TextFieldUtils.setHtmlText(timeText,HtmlText.formatSizeColor(_lang.client("需要天数",{num:obj.totalDays}),0x000000));
			timeText.width = timeText.textWidth + 5;
			timeText.height = timeText.textHeight + 5;
			
			titleText.x = backgroud.height  + 10;
			titleText.y = backgroud.height * .5 - titleText.height - 5;
			timeText.x = backgroud.height + 10;
			timeText.y = backgroud.height * .5 + 5;
			
			addCover();
		}
		
		private function addCover():void
		{
			if(cover == null){
				cover = new Sprite;
				
				cover.graphics.beginFill(0x000000,0);
				cover.graphics.drawRect(0,0,this.width,this.height);
				cover.graphics.endFill();
				
				this.addChildAt(cover,1);
			}
		}
		
		public function set changeLock(func:Function):void
		{
			onChangeThis = func;
		}
		
		public function set changeChooseState(bool:Boolean):void
		{
			lock.visible = !bool;
			lockOpen.visible = bool;
		}
		
		public function set showDetail(func:Function):void
		{
			onShowDetail = func;
		}
		
		public function set deleteThis(func:Function):void
		{
			onDelteThis = func;
		}
		
		public function clear():void
		{
			lockIcon.removeEventListener(MouseEvent.CLICK,openThisHandler);
		}
	}
}